package com.example.dictdemo.controller;

import cn.hutool.core.util.ReflectUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.dictdemo.aspect.annotation.Dict;
import com.example.dictdemo.common.enums.SysUserTypeEnum;
import com.example.dictdemo.common.utils.ResourceUtil;
import com.example.dictdemo.model.vo.DictModel;
import com.example.dictdemo.model.vo.Result;
import com.example.dictdemo.validator.ConstDictRef;
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Data;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * dict控制器
 *
 * @author dlp
 * @date 2024/08/21
 */
@RestController
@RequestMapping("dict")
public class DictController {


    /**
     * 测试字典结果
     *
     * @return {@link Result }<{@link TestDict }>
     */
    @GetMapping("testDictResult")
    public Result<TestDict> testDictResult(){
        TestDict testDict = new TestDict("小孩", 7, "1", "1","57197590443c44f083d42ae24ef26a2c");
        return Result.ok(testDict);
    }


    /**
     * 测试分页字典结果
     *
     * @return {@link Result }<{@link IPage }<{@link TestDict }>>
     */
    @GetMapping("testPageDictResult")
    public Result<IPage<TestDict>> testPageDictResult(){
        IPage<TestDict> page = new Page<>();
        page.setCurrent(1);
        page.setSize(10);
        page.setTotal(10);

        List<TestDict> recordsList = new ArrayList<>();
        recordsList.add(new TestDict("小孩",7,"1","1","4f1765520d6346f9bd9c79e2479e5b12"));
        recordsList.add(new TestDict("小孩哥",13,"2","1","63775228b7b041a99825f79760590b7d"));
        recordsList.add(new TestDict("大哥",17,"2","2","63775228b7b041a99825f79760590b7d"));
        page.setRecords(recordsList);
        return Result.ok(page);
    }


    /**
     * 测试静态字典结果
     *
     * @return {@link Result }<{@link Map }<{@link String }, {@link List }<{@link DictModel }>>>
     */
    @GetMapping("testStaticDictResult")
    public Result<Map<String, List<DictModel>>> testStaticDictResult(){
        Map<String, List<DictModel>> enumDictData = ResourceUtil.getEnumDictData();
        return Result.ok(enumDictData);
    }

    @PostMapping("testValidator")
    public Result<?> testValidator(@Validated @RequestBody TestDict testDict){

        return Result.ok("ok");
    }


    @Data
    static class TestDict{
        private String name;

        private Integer age;

        @JsonFormat(timezone = "GMT+8",pattern = "yyyy-MM-dd HH:mm:ss")
        @DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss")
        private Date nowTime = new Date();

        @Dict(dicCode = "status")
        private String status;
        @Dict(dicCode = "msgType")
        private String msgType;
        @NotNull(message = "[sysUserType]用户类型枚举不能为空")
        @ConstDictRef(constDictClass = SysUserTypeEnum.class,message = "用户类型枚举无效【1：管理员，2：普通操作员，3：普通操作员】")
        @Dict(dicCode = "SysUserType")
        private Integer sysUserType=0;

        @Dict(dictTable ="sys_depart",dicText = "depart_name",dicCode = "id",ds = "slave_2")
        private String depart;

        public TestDict() {

        }

        public TestDict(String name, Integer age, String status, String msgType,String depart) {
            this.name = name;
            this.age = age;
            this.status = status;
            this.msgType = msgType;
            this.depart = depart;
        }
    }

}
